package com.app.motion.robustmotion;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

import com.meituan.robust.Patch;
import com.meituan.robust.PatchExecutor;
import com.meituan.robust.PatchManipulate;
import com.meituan.robust.RobustCallBack;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mBtnLoadPatch;
    private Button mBtnSecond;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnLoadPatch = this.findViewById(R.id.btn_loadpatch);
        mBtnSecond = this.findViewById(R.id.btn_second);
        mBtnLoadPatch.setOnClickListener(this);
        mBtnSecond.setOnClickListener(this);

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x001);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_loadpatch) {
            new PatchExecutor(getApplicationContext(), new PatchManipulateImp(), new Callback()).start();
        }
        if (view.getId() == R.id.btn_second) {
            startActivity(new Intent(this, SecondActivity.class));
        }

    }
}

class PatchManipulateImp extends PatchManipulate {
    @Override
    protected List<Patch> fetchPatchList(Context context) {
        Patch patch = new Patch();
        patch.setName("123");
        patch.setLocalPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "robust" + File.separator + "patch.jar");
        patch.setTempPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "robust" + File.separator + "patch");
        patch.setPatchesInfoImplClassFullName("com.app.motion.robustmotion.patch.PatchesInfoImpl");
        List patches = new ArrayList<Patch>();
        patches.add(patch);
        return patches;
    }

    @Override
    protected boolean verifyPatch(Context context, Patch patch) {
        return true;
    }

    @Override
    protected boolean ensurePatchExist(Patch patch) {
        return true;
    }
}


class Callback implements RobustCallBack {
    @Override
    public void onPatchListFetched(boolean result, boolean isNet, List<Patch> patches) {

    }

    @Override
    public void onPatchFetched(boolean result, boolean isNet, Patch patch) {

    }

    @Override
    public void onPatchApplied(boolean result, Patch patch) {

    }

    @Override
    public void logNotify(String log, String where) {

    }

    @Override
    public void exceptionNotify(Throwable throwable, String where) {

    }
}
